package com.netease.nie.controllers;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.utils.CustomWebUtils;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.dto.GoodsRobRequestDto;
import com.netease.nie.dto.GoodsRobResponseDto;
import com.netease.nie.facade.GoodsRobFacade;

@RestController
@RequestMapping("/api")
public class GoodsRobController extends BaseController {
	
	@Autowired
	private GoodsRobFacade goodsRobFacade;
	
	@RequestMapping(value = "/goods_rob", method = RequestMethod.POST, produces = "application/json")
	public String goodsRob(HttpServletRequest request, @RequestBody GoodsRobRequestDto goodsRobRequestDto) throws Exception {
		Long userId = (Long) request.getAttribute("userId");
		
		if (userId == null)
			throw new BusinessException(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		if (userId <= 0)
			throw new BusinessException(BusinessConstants.INVALID_USER_ID.getCode(), "用户id不合法");
		
		String ip = CustomWebUtils.getIp(request);
		if (StringUtils.isBlank(ip))
			throw new BusinessException(BusinessConstants.USER_IP_ILLEGAL.getCode(), "用户ip不不合法");
		goodsRobRequestDto.setIp(ip);
		goodsRobRequestDto.setUserId(userId);
		GoodsRobResponseDto roodsRobResponseDto = goodsRobFacade.goodsRob(goodsRobRequestDto);
		return jsonSuccess(roodsRobResponseDto);
	}
}
